export const getRandom = (length: number, exclude: number[] = []) => {
  if (length === 0) {
    return 0
  }
  let random = Math.floor(Math.random() * length)
  while (exclude.includes(random)) {
    random = Math.floor(Math.random() * length)
  }
  return random
}

export const seededRandomer = (seed: number) => {
  let x = seed
  return () => {
    x = (Math.imul(1103515245, x) + 12345) & 0x7fffffff
    return x / 2147483648
  }
}

export const shuffleArray = <T>(
  array: T[],
  randomFun: () => number = Math.random
): T[] => {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(randomFun() * (i + 1))
    ;[array[i], array[j]] = [array[j], array[i]]
  }
  return array
}

export const swapElements = (
  array: any[],
  srcIndex: number,
  dstIndex: number
) => {
  const temp = array[srcIndex]
  array[srcIndex] = array[dstIndex]
  array[dstIndex] = temp
}

export const isArraySorted = (arr: number[]): boolean => {
  return arr.every(
    (value, index, array) => index === 0 || array[index - 1] <= value
  )
}
